package com.xinke.edu.mypetadoption.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xinke.edu.mypetadoption.Activity.adapter.ShoppingCartAdapter;
import com.xinke.edu.mypetadoption.databinding.ActivityShoppingCartBinding;
import com.xinke.edu.mypetadoption.util.ShoppingCartManager;
import com.xinke.edu.mypetadoption.Activity.CheckoutActivity;

public class ShoppingCart extends AppCompatActivity {
    ActivityShoppingCartBinding binding;
    private ShoppingCartAdapter shoppingCartAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityShoppingCartBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());

        // 初始化RecyclerView
        initRecyclerView();
        
        // 加载购物车数据
        loadCartData();

        // 点击左上角退出页面返回上一个页面
        binding.exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        // 结算按钮点击事件
        binding.btnCheckout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkout();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 每次进入页面时重新加载购物车数据，确保数据最新
        loadCartData();
    }

    // 初始化RecyclerView
    private void initRecyclerView() {
        shoppingCartAdapter = new ShoppingCartAdapter();
        binding.ShoppingCartItem.setLayoutManager(new LinearLayoutManager(this));
        binding.ShoppingCartItem.setAdapter(shoppingCartAdapter);

        // 设置购物车数据变更监听器
        shoppingCartAdapter.setOnCartItemChangeListener(new ShoppingCartAdapter.OnCartItemChangeListener() {
            @Override
            public void onCartChanged() {
                // 更新全局购物车数据
                ShoppingCartManager.getInstance().setCartItems(shoppingCartAdapter.getCartItems());
                // 更新总价显示
                updateTotalPrice();
            }
        });
    }

    // 加载购物车数据
    private void loadCartData() {
        // 从购物车管理器获取数据
        List<ShoppingCartAdapter.CartItem> cartItems = ShoppingCartManager.getInstance().getCartItems();
        
        // 设置数据
        shoppingCartAdapter.setCartItems(cartItems);
        
        // 初始更新总价
        updateTotalPrice();
    }

    // 更新总价显示
    private void updateTotalPrice() {
        double totalPrice = shoppingCartAdapter.calculateTotalPrice();
        binding.tvTotalPrice.setText("¥" + String.format("%.2f", totalPrice));
    }

    // 结算功能
    private void checkout() {
        if (ShoppingCartManager.getInstance().isEmpty()) {
            Toast.makeText(this, "购物车为空，无法结算", Toast.LENGTH_SHORT).show();
            return;
        }

        // 跳转到结算页面
        Intent intent = new Intent(ShoppingCart.this, CheckoutActivity.class);
        startActivity(intent);
    }
}